﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;

namespace Core.Domain
{
    [DataContract]
    public class Culture : BaseEntity<byte>
    {
        public Culture()
        {
        }    

        [DataMember]
        public override byte Id { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Code { get; set; }

        public override bool Validate()
        {
            BrokenRules.Clear();
            ValidateCultureCode();
            ValidateNotADemoCulture();

            var isValid = (BrokenRules.Count == 0);
            return isValid;
        }

        private void ValidateCultureCode()
        {        
            try
            {
                CultureInfo ci = new CultureInfo(Code);
            }
            catch
            {             
                BrokenRules.Add(new Library.Domain.ValidationRule("Code", "Invalid Culture Code"));
            }         
        }

        private void ValidateNotADemoCulture()
        {
            if (this.Name.Equals("English") || this.Name.Equals("Romana"))
                BrokenRules.Add(new Library.Domain.ValidationRule("Demo", string.Format("Language \"{0}\" can't be modified. It's used for demos.", this.Name)));
        }
    }
}

